package com.app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.adapter.NewsListAdapter;
import com.app.bean.NewsListBean;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class NewsListActivity extends AppCompatActivity {

    private ListView newsListLv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);
        initView();
        initData();
        initEvent();
    }
    private void initView() {
        newsListLv = (ListView) findViewById(R.id.news_list_listview);
    }
    private void initData(){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id","1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://www.xhjjyst.com/freshmenapp/news/list";
        RequestQueue requestQueue= Volley.newRequestQueue(NewsListActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                NewsListBean newsListBean = gson.fromJson(jsonObject.toString(),NewsListBean.class);

                List<NewsListBean.ListBean> listBeen = newsListBean.getList();

                newsListLv.setAdapter(new NewsListAdapter(NewsListActivity.this, listBeen));

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(NewsListActivity.this,"网络异常",Toast.LENGTH_SHORT).show();

            }
        });
        requestQueue.add(jsonObjectRequest);

    }
    private void initEvent(){

    }
}
